<?php
include 'function.php';
$error = '';
if (isset($_POST['email']))
{
	$conn = conn();
	$email = trim($_POST['email']);
	if (!empty($email))
	{
		if (!preg_match('/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/', $email))
		{
			$error = '邮箱格式不正确';
		}
		else
		{
			$esc_email = mysql_real_escape_string($email, $conn);
			$sql = "SELECT id FROM accounts WHERE email = '$esc_email'";
			$result = mysql_query($sql, $conn);
			$row = mysql_fetch_assoc($result);
			if (!isset($row['id']))
			{
				$error = '这个邮箱没有注册过';
			}
			else
			{
				// send email of resetting password to user
			}
		}
	}
	else 
	{
		$error = '邮箱不能为空';
	}
}
?><!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link rel="stylesheet" type="text/css" href="/theme.css" />
    </head>
    <body>
        <div class="wrapper">
        	<div class="logo"><a href="/"><?php echo SITE;?></a></div>
        	<div class="clear"></div>
		<form action="password.php" method="post">
			<div class="reg_left" style="height:50px;"><h1 style="margin:0px;">重设密码</h1></div>
			<div class="reg_right"></div>
			<div class="clear"></div><?php if(isset($_POST['email']) && '' == $error){// email is sent?>
			<div style="margin-left:180px;margin-top:10px;">请到 <?php echo $email;?> 查阅来自<?php echo SITE;?>的邮件，从邮件重设你的密码。
			<br />
			<a href="http://mail.<?php 
			$domain = strtolower(substr($email, strpos($email, '@') + 1));
			if (0 === strpos($domain, 'vip.'))
			{
				$domain = substr($domain, 4);
			}
			if ('gmail.com' == $domain)
				echo 'google.com';
			else 
				echo $domain;
			?>" target="_blank"><?php 
			$box_name = null;
			$domains = array('qq' => 'QQ', 'sina' => '新浪', '163' => '网易', 'gmail' => 'Gmail', 'sohu' => '搜狐', 'yahoo' => '雅虎'); 
			foreach($domains as $site_domain => $site_name)
			{
				if(preg_match('/(^|\.)'.$site_domain.'\./', $domain))
				{
					$box_name = $site_name;
					break;
				}
			}
			if (!$box_name)
			{
				$box_name = substr($domain, 0, strpos($domain, '.'));
			}
			?>登录<?php echo $box_name;?>邮箱查收确认信</a></div>
			<?php } else {?><div class="reg_left">邮箱：</div>
			<div class="reg_right"><input name="email" type="text" class="reg_input" value="<?php if(isset($email)) echo $email; ?>" /></div>
			<div class="clear"></div><?php if('' != $error) echo '
			<div class="reg_left"></div>
			<div class="reg_right" style="margin-top:10px;"><span style="color:red;">'.$error.'</span></div>
			<div class="clear"></div>
			';?>
			<div class="reg_left"></div>
			<div class="reg_right"><input type="submit" value="重设密码" /></div>
			<div class="clear"></div><?php }// else end?>
		</form>
		<div class="reg_left"></div>
		<div class="reg_right"><div style="margin-top:40px;">还没有<?php echo SITE;?>帐号？<a href="register.php">立即注册</a></div></div>
		<div class="clear"></div>
        </div>
    </body>
</html>